#include "directorieswidget.h"

DirectoriesWidget::DirectoriesWidget(QWidget *parent) : QWidget(parent)
{
    tabWidget = new QTabWidget;
    binListWidget = new QListWidget;
    libListWidget = new QListWidget;
    cIncludeListWidget = new QListWidget;
    cppIncludeListWidget = new QListWidget;

    pathLineEdit = new QLineEdit;
    pathButton = new QPushButton; //定义path按钮
    addButton = new QPushButton(tr("Add")); //定义add按钮
    replaceButton = new QPushButton(tr("Replace")); //定义replace按钮
    deleteButton = new QPushButton(tr("Delete")); //定义delete按钮

    QVBoxLayout *mainLayout = new QVBoxLayout;
    this->setLayout(mainLayout);

    tabWidget->addTab(binListWidget, tr("Binaries"));
    tabWidget->addTab(libListWidget, tr("Libraries"));
    tabWidget->addTab(cIncludeListWidget, tr("C Includes"));
    tabWidget->addTab(cppIncludeListWidget, tr("C++ Includes"));

    binListWidget->setSpacing(3);
    binListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    binListWidget->setDragEnabled(true);
    binListWidget->viewport()->setAcceptDrops(true);
    binListWidget->setDropIndicatorShown(true);//设置显示将要被放置的位置
    binListWidget->setDragDropMode(QAbstractItemView::InternalMove);//设置拖放模式为移动项目，如果不设置，默认为复制项目
    connect(binListWidget, &QListWidget::itemSelectionChanged, this, &DirectoriesWidget::itemSelectionChanged);

    libListWidget->setSpacing(3);
    libListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    libListWidget->setDragEnabled(true);
    libListWidget->viewport()->setAcceptDrops(true);
    libListWidget->setDropIndicatorShown(true);//设置显示将要被放置的位置
    libListWidget->setDragDropMode(QAbstractItemView::InternalMove);//设置拖放模式为移动项目，如果不设置，默认为复制项目
    connect(libListWidget, &QListWidget::itemSelectionChanged, this, &DirectoriesWidget::itemSelectionChanged);

    cIncludeListWidget->setSpacing(3);
    cIncludeListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    cIncludeListWidget->setDragEnabled(true);
    cIncludeListWidget->viewport()->setAcceptDrops(true);
    cIncludeListWidget->setDropIndicatorShown(true);//设置显示将要被放置的位置
    cIncludeListWidget->setDragDropMode(QAbstractItemView::InternalMove);//设置拖放模式为移动项目，如果不设置，默认为复制项目
    connect(cIncludeListWidget, &QListWidget::itemSelectionChanged, this, &DirectoriesWidget::itemSelectionChanged);

    cppIncludeListWidget->setSpacing(3);
    cppIncludeListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    cppIncludeListWidget->setDragEnabled(true);
    cppIncludeListWidget->viewport()->setAcceptDrops(true);
    cppIncludeListWidget->setDropIndicatorShown(true);//设置显示将要被放置的位置
    cppIncludeListWidget->setDragDropMode(QAbstractItemView::InternalMove);//设置拖放模式为移动项目，如果不设置，默认为复制项目
    connect(cppIncludeListWidget, &QListWidget::itemSelectionChanged, this, &DirectoriesWidget::itemSelectionChanged);

    connect(tabWidget, &QTabWidget::currentChanged, this, &DirectoriesWidget::currentTabChanged);

    connect(pathLineEdit, &QLineEdit::textChanged, this, &DirectoriesWidget::pathLineEditTextChanged);
    connect(pathLineEdit, &QLineEdit::returnPressed, this, &DirectoriesWidget::lineEditEnterKeyPressed);

    pathButton->setIcon(QPixmap(":images/path.png"));
    pathButton->setToolTip(tr("Add a path"));
    pathButton->setFocusPolicy(Qt::NoFocus);
    QHBoxLayout *pathHLayout = new QHBoxLayout;
    pathHLayout->addWidget(pathLineEdit);
    pathHLayout->addWidget(pathButton);

    connect(pathButton, &QPushButton::clicked, this, &DirectoriesWidget::pathButtonClicked);

    QHBoxLayout *buttonHLayout = new QHBoxLayout;
    buttonHLayout->addStretch();
    buttonHLayout->addWidget(addButton);
    buttonHLayout->addWidget(replaceButton);
    buttonHLayout->addWidget(deleteButton);
    buttonHLayout->addStretch();

    connect(addButton, &QPushButton::clicked, this, &DirectoriesWidget::addButtonClicked);
    connect(deleteButton, &QPushButton::clicked, this, &DirectoriesWidget::deleteButtonClicked);
    connect(replaceButton, &QPushButton::clicked, this, &DirectoriesWidget::replaceButtonClicked);

    addButton->setEnabled(false);
    replaceButton->setEnabled(false);
    deleteButton->setEnabled(false);

    addButton->setFocusPolicy(Qt::NoFocus);
    replaceButton->setFocusPolicy(Qt::NoFocus);
    deleteButton->setFocusPolicy(Qt::NoFocus);

    mainLayout->addWidget(tabWidget);
    mainLayout->addLayout(pathHLayout);
    mainLayout->addLayout(buttonHLayout);
}

void DirectoriesWidget::itemSelectionChanged()
{
    QListWidget *listWidget = dynamic_cast<QListWidget *>(sender());
    if (listWidget->selectedItems().size() > 0)
    {
        QString path = pathLineEdit->text().trimmed();
        if (!path.isEmpty())
            replaceButton->setEnabled(true);
        else
            replaceButton->setEnabled(false);

        deleteButton->setEnabled(true);
    }
    else
    {
        replaceButton->setEnabled(false);
        deleteButton->setEnabled(false);
    }
}

void DirectoriesWidget::currentTabChanged(int index)
{
    addButton->setEnabled(false);
    replaceButton->setEnabled(false);
    deleteButton->setEnabled(false);
    pathLineEdit->clear();
    QWidget *currentWidget = tabWidget->widget(index);
    QListWidget *currentListWidget = dynamic_cast<QListWidget *>(currentWidget);
    currentListWidget->clearSelection();
}

void DirectoriesWidget::pathButtonClicked()
{
    QString selectedPath = QFileDialog::getExistingDirectory(this, tr("Select a folder"), "");
    if (selectedPath.isEmpty()) return;

    QWidget *currentWidget = tabWidget->currentWidget();
    QListWidget *currentListWidget = dynamic_cast<QListWidget *>(currentWidget);

    currentListWidget->addItem(selectedPath);
    int itemCount = currentListWidget->count();
    if (itemCount > 0)
        currentListWidget->item(itemCount - 1)->setFlags(Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled);
}

void DirectoriesWidget::addButtonClicked()
{
    QWidget *currentWidget = tabWidget->currentWidget();
    QListWidget *currentListWidget = dynamic_cast<QListWidget *>(currentWidget);

    QString text = pathLineEdit->text().trimmed();
    if (!text.isEmpty())
    {
        currentListWidget->addItem(text);
        pathLineEdit->setText("");
        int itemCount = currentListWidget->count();
        if (itemCount > 0)
            currentListWidget->item(itemCount - 1)->setFlags(Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled);
    }
}

void DirectoriesWidget::replaceButtonClicked()
{
    QWidget *currentWidget = tabWidget->currentWidget();
    QListWidget *currentListWidget = dynamic_cast<QListWidget *>(currentWidget);
    QList<QListWidgetItem *> selectedItems = currentListWidget->selectedItems();
    QString path = pathLineEdit->text().trimmed();
    if (selectedItems.size() > 0 && !path.isEmpty())
    {
        selectedItems[0]->setText(path);
    }
}

void DirectoriesWidget::deleteButtonClicked()
{
    QWidget *currentWidget = tabWidget->currentWidget();
    QListWidget *currentListWidget = dynamic_cast<QListWidget *>(currentWidget);

    QList<QListWidgetItem *> selectedItems = currentListWidget->selectedItems();
    if (selectedItems.size() > 0)
    {
        currentListWidget->removeItemWidget(selectedItems[0]);
        delete selectedItems[0];
    }
}

void DirectoriesWidget::pathLineEditTextChanged()
{
    QString path = pathLineEdit->text().trimmed();

    QWidget *currentWidget = tabWidget->currentWidget();
    QListWidget *currentListWidget = dynamic_cast<QListWidget *>(currentWidget);

    if (!path.isEmpty())
        addButton->setEnabled(true);
    else
        addButton->setEnabled(false);

    if (!path.isEmpty() && currentListWidget->selectedItems().size() > 0)
        replaceButton->setEnabled(true);
    else
        replaceButton->setEnabled(false);
}

void DirectoriesWidget::lineEditEnterKeyPressed()
{
    addButtonClicked();
}
